<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {
    
    public function __construct()
    {
        parent::__construct();
    }
    
    // ------------------------------------------------------------------------
    
    public function count_users()
    {
        $query = $this->db->select('user_id')
                          ->where('deleted', 'n')
                          ->get('users');
                          
        return $query->num_rows();
    }
    
    // ------------------------------------------------------------------------
    
    public function list_users($num = '', $offset = '', $row = '', $order = 'asc', $where = '')
    {
        $offset = $offset > 0 ? ($offset - 1) * $num : 0;
        
        if (! $row) $row = 'created';
        
        $query = $this->db->select()
						  ->join('roles', 'roles.role_id = users.role_id', 'left')
						  ->where('users.deleted', 'n')
                          ->order_by($row, $order)
                          ->get('users', $num, $offset);
                          
        return $query->num_rows() > 0 ? $query->result_array() : FALSE;
    }
    
    // ------------------------------------------------------------------------
	
	public function get_user($value = '', $where = 'users.user_id')
	{
		$query = $this->db->select('users.*, roles.*, GROUP_CONCAT(user_websites.site_code) AS user_websites')
						  ->join('roles', 'roles.role_id = users.role_id', 'left')
						  ->join('user_websites', 'user_websites.user_id = users.user_id', 'left')
						  ->where($where, $value)
						  ->where('users.deleted', 'n')
						  ->get('users', 1);
						  
		return $query->num_rows() > 0 ? $query->row_array() : FALSE;
	}
	
	// ------------------------------------------------------------------------
    
    public function delete_user($user_id = '')
    {
        $query = $this->db->where('user_id', $user_id)
                          ->where('deleted', 'n')
                          ->update('users', array('deleted' => 'y'));
                          
        return TRUE;
    }
    
    // ------------------------------------------------------------------------
    
}

/* End of file user_model.php */
/* Location: ./application/models/user_model.php */